package com.kaiwukj.android.ufamily.mvp.ui.page.keeper;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.StringUtils;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerKeeperComponent;
import com.kaiwukj.android.ufamily.di.module.KeeperModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.KeeperOrderResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.KeeperResult;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.KeeperOrderCreateUrl)
public class KeeperOrderCreateActivity extends BaseMvpActivity<KeeperPresenter> implements KeeperContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_flag)
    TextView tvServiceName;
    @BindView(R.id.et_input)
    EditText etInput;
    @BindView(R.id.tv_appoint_time)
    TextView tvAppointTime;
    @BindView(R.id.tv_time_msg)
    TextView tvTimeMsg;

    @Autowired
    int keeperId;
    @Autowired
    int serviceId;
    @Autowired
    String serviceName;
    @Autowired
    String startTime;
    @Autowired
    String endTime;

    private String appointTime;


    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }


    @OnClick(R.id.container_appoint)
    void pickTime() {
        KeyboardUtils.hideSoftInput(this);
        boolean[] types = new boolean[]{true, true, true, true, true, false};
        Calendar calendarLater = Calendar.getInstance();
        calendarLater.add(Calendar.YEAR, 1);
        TimePickerBuilder timePickerBuilder = new TimePickerBuilder(this, (date, v2) -> {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            tvAppointTime.setText(simpleDateFormat.format(date.getTime()));
            appointTime = tvAppointTime.getText().toString();
        }).setRangDate(Calendar.getInstance(), calendarLater)
                .setOutSideCancelable(true)
                .setCancelColor(R.color.common_text_slight_color)
                .isCyclic(false)
                .setLineSpacingMultiplier(1.8f)
                .setSubmitColor(R.color.app_color_theme)
                .setType(types);
        timePickerBuilder.build().show();
    }

    @OnClick(R.id.btn_confirm)
    void submit() {

        if (keeperId <= 0) {
            showMessage("管家信息获取失败!");
            return;
        }
        if (serviceId <= 0) {
            showMessage("服务信息获取失败!");
            return;
        }
        String remarks = etInput.getText().toString();
        if (StringUtils.isEmpty(remarks.trim())) {
            showMessage("请填写地址信息");
            return;
        }
        if (StringUtils.isEmpty(appointTime)) {
            showMessage("请选择上门时间");
            return;
        }
        mPresenter.createOrder(keeperId, serviceId, appointTime, remarks);

    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerKeeperComponent.builder()
                .appComponent(appComponent)
                .keeperModule(new KeeperModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_keeper_order_create;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setVisibility(View.GONE);
        tvServiceName.setText(serviceName);
        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
            tvTimeMsg.setVisibility(View.VISIBLE);
            tvTimeMsg.setText(String.format("可预约时间:%s-%s", startTime, endTime));
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    public void onTaskSuccess(int taskId) {
        super.onTaskSuccess(taskId);
        if (taskId == KeeperPresenter.ORDER_CREATE) {
            new Handler().postDelayed(() -> {
                setResult(RESULT_OK);
                finish();
            }, 1500);
        }
    }

    @Override
    public void showKeeperInfo(KeeperResult result) {

    }

    @Override
    public void showKeeperList(List<KeeperResult> results) {

    }

    @Override
    public void showKeeperOderList(List<KeeperOrderResult> results) {

    }
}
